/**
 *@Description
 *@author your name
 *@createDate 2011-7-5 上午06:15:11
 *@version V1.0
 */
package com.welldone.service.tables;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ResultSetExtractor;

import com.welldone.common.CommonData;
import com.welldone.service.DBService;

public class TableManager {

	/**
	 * 添加一张桌子，或一个包间
	 *@author your name
	 *@createDate 2011-7-5
	 *@param map
	 *@return
	 */
	public boolean addTable(Map map){
		boolean flag = false;
		String sql="insert into t_table(tableId,position,tableName,tableStatus) values(?,?,?,?,?,?)";
		String tableId = UUID.randomUUID().toString();
		Object[] args = {tableId,map.get("position"),map.get("tableName"),map.get("tableStatus")};
		int result = DBService.getJdbcTemplate().update(sql,args);
		if(result!=-1){
			flag = true;
		}
		return flag;
	}
	/**
	 * 更新一张桌子，或一个包间的内容
	 *@author your name
	 *@createDate 2011-7-5
	 *@param uMap
	 *@return
	 */
	public boolean updateTable(Map map){
		boolean flag = false;
		String sql="update t_table set position=?,tableName=?,tableStatus=? where tableId = ?";
		Object[] args = {map.get("position"),map.get("tableName"),map.get("tableStatus"),map.get("tableId")};
		int result = DBService.getJdbcTemplate().update(sql,args);
		if(result!=-1){
			flag = true;
		}
		return flag;
	}
	/**
	 * 删除一张桌子或包间
	 *@author your name
	 *@createDate 2011-7-5
	 *@param tableId
	 *@return
	 */
	public boolean deleteTable(String tableId){
		boolean flag = false;
		String sql = "delete from t_table where tableId = ?";
		Object[] args = {tableId};
		int result = DBService.getJdbcTemplate().update(sql,args);
		if(result!=-1){
			flag  = true;
		}
		return flag;
	}
	
	/**
	 * 更新桌子或包间的状态
	 *@author your name
	 *@createDate 2011-7-5
	 *@param tableId
	 *@param status
	 *@return
	 */
	public boolean updateTableStatus(String tableId,String status){
		boolean flag = false;
		String sql="update t_table set tableStatus=? where tableId = ?";
		String talbeStatus="3";
		if(status.equals(CommonData.TABLESTATUS_0)){
			talbeStatus = status;
		}
		if(status.equals(CommonData.TABLESTATUS_1)){
			talbeStatus = status;
		}
		if(status.equals(CommonData.TABLESTATUS_2)){
			talbeStatus = status;
		}
		if(status.equals(CommonData.TABLESTATUS_3)){
			talbeStatus = status;
		}
		if(status.equals(CommonData.TABLESTATUS_4)){
			talbeStatus = status;
		}
		Object[] args = {talbeStatus,tableId};
		int result = DBService.getJdbcTemplate().update(sql,args);
		if(result!=-1){
			flag = true;
		}
		return flag;
	}
	
	/**
	 * 更新桌子开台，关台的时间
	 *@author your name
	 *@createDate 2011-7-5
	 *@param tableId
	 *@param startTime
	 *@param endTime
	 *@return
	 */
	public boolean upateTableTime(String tableId,String startTime,String endTime){
		boolean flag = false;
		String sql="";
		Object[] args = new Object[2];
		if(null!=startTime){
			sql = "update t_table set startTime = ? where tableId = ?";
			args[0] = startTime;
			args[1] = tableId;
		}
		if(null!=endTime){
			sql = "update t_table set endTime = ? where tableId = ?";
			args[0] = endTime;
			args[1] = tableId;
		}
		int result = DBService.getJdbcTemplate().update(sql,args);
		if(result!=-1){
			flag = true;
		}
		return flag;
		
	}
	
	/**
	 * 得到所有桌子，或包间
	 *@author your name
	 *@createDate 2011-7-6
	 *@return
	 */
	public List getAllTable(){
		String sql = "select * from t_table";
		return DBService.getJdbcTemplate().queryForList(sql);
	}
	
	/**
	 * 根据位置查询桌子，或包间
	 *@author your name
	 *@createDate 2011-7-6
	 *@param positionId
	 *@return
	 */
	public List getTableByPosition(String positionId){
		String sql = "select * from t_table where position = ?";
		Object args[] = {positionId};
		return DBService.getJdbcTemplate().queryForList(sql, args);
	}
	
	/**
	 * 根据桌子，或包间ID，查询详细信息
	 *@author your name
	 *@createDate 2011-7-6
	 *@param tableId
	 *@return
	 */
	public Map getTableById(String tableId){
		String sql = "select * from t_table where tableId = ?";
		Object[] args = {tableId};
		return (Map)DBService.getJdbcTemplate().query(sql, args, new ResultSetExtractor() {
			
			@Override
			public Object extractData(ResultSet rs) throws SQLException,
					DataAccessException {
				Map map = null;
				while(rs.next()){
					map = new HashMap();
					map.put("tableId", rs.getString("tableId"));
					map.put("position", rs.getString("position"));
					map.put("tableName", rs.getString("tableName"));
					map.put("tableStatus", rs.getString("tableStatus"));
					map.put("startTime", rs.getDate("startTime"));
					map.put("endTime", rs.getDate("endTime"));
				}
				return map;
			}
		});
	}
	
	
	
	
	
	public static void main(String args[]){
		Map map = new HashMap();
		map.put("position", "大厅");
		map.put("tableName", "大厅30号桌");
		map.put("tableStatus", "0");
		map.put("startTime", "2011-07-05 06:28:34");
		map.put("endTime", "2011-07-05 09:28:34");
		new TableManager().addTable(map);
	}
}


